package Z11_4_synchronizedControl;

import lombok.extern.slf4j.Slf4j;

/**
 * @author： pang-yun
 * @date： 2021-11-04 14:26
 * 功能目的 ：用于 synchronized 控制 thread 先后输出
 */

@Slf4j
public class OrderControl {
    static Object lock = new Object();
    static boolean useFirst = false;

    public static void main(String[] args) {
        Thread first = new Thread(() -> {
          synchronized (lock){
              useFirst = true;
              lock.notifyAll();
              log.info("first----");
          }
        }, "first");

        Thread second = new Thread(() -> {
           synchronized (lock){
               while (!useFirst){
                   try {
                       lock.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }

               log.info("second----");
           }
        }, "second");

        second.start();
        first.start();
    }
}
